#! /usr/bin/python3

import os
import subprocess

panelAddress = ""
panelTag = ""
srcDir = "/data/changhong/edge_midware"
destDir = "/home/lhy/smarthome/edge_rk3308/software/query_config_ble_synergy_sites/cmakeShell/log"

files2pullDict = {
    0:"panelConfig.txt",
    1:"whitedevs-i.txt",
    2:"rules-config.txt"
}

def printDisplayInfo():
    print("****************")
    for file in files2pullDict.items():
        print(f" {file}")
    print("****************")


def pullAndShow(file2pull):
    global panelAddress, panelTag
    if(len(panelAddress) == 0):
        panelTag = ""
    else:
        panelTag = f"-s {panelAddress}"
    
    # 拉取文件并显示其内容
    os.system(f"adb {panelTag} shell cat {srcDir}/{file2pull}")
    os.system(f"adb {panelTag} pull {srcDir}/{file2pull} {destDir}")
   

# 连接ADB
def adbConnect() -> bool:
    global panelAddress
    os.system("adb devices")
    panelAddress = input("please input panel address: ")
    if len(panelAddress) != 0:
        os.system(f"adb connect {panelAddress}")
        code, output = subprocess.getstatusoutput(f"adb -s {panelAddress} shell byjs2023")
    else:
        code, output = subprocess.getstatusoutput(f"adb shell byjs2023")

    if(code != 0):
        print("adb shell failed ...")
        exit
    else:
        return True


if __name__ == "__main__":
    adbConnect()
    while True:
        printDisplayInfo()
        sites2push = input("choose files to show and pull: ")
        siteIndexs = sites2push.split(' ')
        for siteIndex in siteIndexs:
            pullAndShow(files2pullDict[int(siteIndex)])




